package com.kingdee.hackson.utils;


import java.util.ArrayList;
import java.util.List;

/**
 * 这里所有的id都是短ID
 */
public class OrgCodeConvertUtils {

    public static String formatTo26Code(int data) {
        return formatTo26Code(data, 26, 2);
    }

    public static Long decode26Code(String levelCode) {
        String code = levelCode;
        int length = code.length();
        String replaceAll = code.replaceAll("0", "");
        char[] chars = replaceAll.toCharArray();
        Long result = null;
        for (int i = 0; i < chars.length; i++) {
            long temp = chars[i] - 'a';
            if (result == null || result == 0) {
                result = temp;
                continue;
            }
            result = result * 4 + temp;
        }
        return result;
    }

    public static Long getParentIdBy26Code(String levelCode) {
        int length = levelCode.length();
        if (length == 2) {
            return -1L;
        }
        String substring = levelCode.substring(0, length - 2);
        return decode26Code(substring);
    }

    private static String formatTo26Code(int data, int unit, int length) {
        if (data == 0) {
            return "0a";
        }
        Integer[] arrays = getArrays(data, 4);
        StringBuilder sb = new StringBuilder("0a");
        for (int i = arrays.length - 1; i >= 0; i--) {
            Integer array = arrays[i];
            String extracted = convert(array, 26);
            sb.append(format(extracted, length));
        }
        return sb.toString();
    }

    private static String format(String extracted, int length) {
        String format = String.format("%" + length + "s", extracted);
        return format.replaceAll(" ", "0");
    }

    private static String convert(int data, int unit) {
        if (data == 0) {
            return String.valueOf(getChar(data));
        }
        StringBuilder result = new StringBuilder();
        while (data / unit > 0) {
            int i = data % unit;
            result.append(getChar(i));
            data = data / unit;
        }
        if (data > 0) {
            result.append(getChar(data));
        }
        return result.reverse().toString();
    }

    private static Integer[] getArrays(int data, int unit) {
        List<Integer> result = new ArrayList<>();
        while (data / unit > 0) {
            int i = data % unit;
            int temp = data;
            data = data / unit;
            result.add(i);
        }
        result.add(data);
        return result.toArray(new Integer[1]);
    }

    private static char getChar(int data) {
        return (char) (97 + data);
    }

    public static void main(String[] args) {
        // 测试编码和解码
        int data = 0;
        String formatCode = formatTo26Code(data);
        Long decode = decode26Code(formatCode);
        System.out.println("data = " + data + ",formatCode=" + formatCode + ",decode=" + decode);

        // 测试接触父级编码
        String code = "0b";
        String childCode = "0b0a";
        Long parentId = decode26Code(code);
        Long childId = decode26Code(childCode);
        Long parentIdBy26Code = getParentIdBy26Code(childCode);
        System.out.println("parentId= " + parentId + ",childId=" + childId + ",parentIdBy26Code=" + parentIdBy26Code);
        Long aLong = IDConvertUtils.encodeToLongId(parentId);
        System.out.println("encodeToLongId = " + aLong);
    }
}